package com.weiaibenpao.demo.chislim.ui.activity.me;

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.orhanobut.logger.Logger;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseActivity;
import com.weiaibenpao.demo.chislim.bean.SportHistoryBean;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.presenter.SportHistoryPresenter;
import com.weiaibenpao.demo.chislim.ui.adapter.SportHistoryAdapter;
import com.weiaibenpao.demo.chislim.ui.widget.CustomTitleBar;
import com.weiaibenpao.demo.chislim.view.SportHistoryView;

import java.util.ArrayList;

import butterknife.BindView;

public class SportHistoryActivity extends BaseActivity<SportHistoryView, SportHistoryPresenter> implements SportHistoryView,BaseQuickAdapter.RequestLoadMoreListener {
    public static final int SPORT_HISTORY_PAGE_SIZE = 2;//后台建议每页size使用2
    @BindView(R.id.myTitle)
    CustomTitleBar myTitle;
    @BindView(R.id.myMatch)
    RecyclerView myMatch;
    SportHistoryAdapter sportHistoryAdapter;
    @BindView(R.id.allDis)
    TextView allDis;
    @BindView(R.id.allTime)
    TextView allTime;
    @BindView(R.id.allPace)
    TextView allPace;
    @BindView(R.id.allNum)
    TextView allNum;

    private int mNextPage = 0;
    private View emptyView;

    @Override
    public int setLayoutResourceID() {
        return R.layout.activity_sport_history;
    }

    @Override
    public void init(Bundle savedInstanceState) {
        initFonts();

        mPresenter.getDjqMall(UserPreferences.getInstance().getInt(UserPreferences.USER_ID), mNextPage, SPORT_HISTORY_PAGE_SIZE);
        sportHistoryAdapter = new SportHistoryAdapter(this);
        sportHistoryAdapter.setOnLoadMoreListener(this,myMatch);
        myMatch.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        myMatch.setAdapter(sportHistoryAdapter);
        myTitle.setLeftImageOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        emptyView = LayoutInflater.from(this).inflate(R.layout.layout_empty, myMatch,false);
        ((TextView) emptyView.findViewById(R.id.tv_empty_new)).setText("这个人很懒，没有留下任何足迹！");

    }

    private void initFonts() {
        Typeface typeFace = Typeface.createFromAsset(this.getAssets(), "fonts/Reducto Condensed SSi Condensed.ttf");
        allDis.setTypeface(typeFace);
        allTime.setTypeface(typeFace);
        allPace.setTypeface(typeFace);
        allNum.setTypeface(typeFace);
    }

    @Override
    public void sportHistorySuccess(SportHistoryBean sportHistoryBean){
        Logger.e("ljb:sportHistorySuccess");
        initView(sportHistoryBean);
        setData((ArrayList<SportHistoryBean.ListBean>) sportHistoryBean.getList());
        mNextPage++;
    }

    private void setData(ArrayList<SportHistoryBean.ListBean> data) {
        final int size = data == null ? 0 : data.size();
        if (size > 0) {
            sportHistoryAdapter.addData(data);
        }else {
            sportHistoryAdapter.setEmptyView(emptyView);
        }
        if (size < SPORT_HISTORY_PAGE_SIZE) {
            sportHistoryAdapter.loadMoreEnd();
        } else {
            sportHistoryAdapter.loadMoreComplete();
        }
    }

    public void initView(SportHistoryBean sportHistoryBean){
        allDis.setText(String.valueOf(sportHistoryBean.getSumDistance()));
        allTime.setText(sportHistoryBean.getSumTime() + "");
        allPace.setText(sportHistoryBean.getRunEveSpace());
        allNum.setText(sportHistoryBean.getAllSportNumber()+"");
    }



    @Override
    public SportHistoryPresenter initPresenter() {
        return new SportHistoryPresenter();
    }

    @Override
    public void onLoadMoreRequested() {
        mPresenter.getDjqMall(UserPreferences.getInstance().getInt(UserPreferences.USER_ID), mNextPage, SPORT_HISTORY_PAGE_SIZE);
    }
}
